package cn.szzsi._admin;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.OperateLogs;
import cn.szzsi.common.model.Orders;
import cn.szzsi.common.model.UserBalanceDetail;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yuanzheng on 2016/12/6.
 */
public class RewardsController extends BaseAdminController<OperateLogs> {
    private String shopId;
    public void list(){
        render("list.html");
    }
    //ajax list
    public void ajaxlist(){

        String sql = "SELECT bbm.id,bbm.mid,bbm.addMoney,bbm.fromuid,bbm.level,bbm.rate,bbm.createDate,bbm.type,m.username AS tousername FROM mall_user_balance_detail bbm LEFT JOIN mall_member m ON bbm.mid = m.id where bbm.type='0' ORDER BY createDate DESC";
            sql = "SELECT bbmm.*, mm.username AS fromusername FROM ("+sql+") bbmm LEFT JOIN mall_member mm ON bbmm.fromuid = mm.id ORDER BY bbmm.createDate DESC,bbmm. LEVEL ASC";

        List<UserBalanceDetail> bms = UserBalanceDetail.dao.find(sql);

        renderJson(bms);
    }

    public void personlist(){
        render("personlist.html");
    }

    public void ajaxpersonlist(){
        shopId=getAttr("shopId");
        String sql = "SELECT bb.mid,(sub.balance+sub.cashing)AS total,(sub.balance-sub.freezeMoney)availNum,sub.cashing hasDrawNum,sub.freezeMoney freezeNum,m.username AS username,m.realname,m.code,m.phone,m.memberRankId AS rankid FROM mall_user_balance_detail bb LEFT JOIN mall_member m ON bb.mid = m.id LEFT JOIN mall_user_balance sub on m.id=sub.mid WHERE (sub.balance+sub.cashing)>0 and (sub.balance - sub.freezeMoney)>=0 and sub.shopId='"+shopId+"' GROUP BY bb.mid ";
        Map<String,Object> result = new HashMap<String,Object>();

        List<Record> lr = Db.find(sql);
        Orders orders= Orders.dao.findFirst("select sum(totalAmount) totalAmount from mall_orders where paymentStatus='2' and uid=?",getLoginMember().getStr("id"));
        System.out.println("select sum(totalAmount) totalAmount from mall_orders where paymentStatus='2' and uid="+getLoginMember().getStr("id")+"");
        BigDecimal allTotalAmount=orders.getBigDecimal("totalAmount");
        if(allTotalAmount==null || "0".equals(allTotalAmount.toString())){
            allTotalAmount= new BigDecimal(0);
        }
        /**
         * 下面sql待改进(后续会去除)
         */
        sql = "SELECT bb.mid,(sub.balance+sub.cashing)AS total,(sub.balance-sub.freezeMoney)availNum,sub.cashing hasDrawNum,sub.freezeMoney freezeNum,m.username AS username,m.realname,m.code,m.phone,m.memberRankId AS rankid FROM mall_user_balance_detail bb LEFT JOIN mall_member m ON bb.mid = m.id LEFT JOIN mall_user_balance sub on m.id=sub.mid WHERE (sub.balance+sub.cashing)>0 and sub.shopId='"+shopId+"' GROUP BY bb.mid ";
        String totalSql="select sum(total) total,sum(hasDrawNum)totalHasDrawNum,sum(availNum)totalAvail ,sum(freezeNum)totalFreeze from("+sql+") pid";
        System.out.println(totalSql);
        Record totalRecord= Db.findFirst(totalSql);
        BigDecimal total=totalRecord.getBigDecimal("total");//总奖金(总)
        BigDecimal totalHasDrawNum=totalRecord.getBigDecimal("totalHasDrawNum");//已提现奖金(总)
        BigDecimal totalAvail=totalRecord.getBigDecimal("totalAvail");//未提现可用奖金(总)
        BigDecimal totalFreeze=totalRecord.getBigDecimal("totalFreeze");//冻结奖金(总)

        result.put("allTotalAmount",allTotalAmount);//总营业额
        result.put("total",total);//总奖金
        result.put("totalHasDrawNum",totalHasDrawNum);//已提现奖金
        result.put("totalAvail",totalAvail);//未提现可用奖金
        result.put("totalFreeze",totalFreeze);//冻结奖金
        result.put("data",lr);
        renderJson(result);
    }

    /*public void ajaxpersonlist(){
        String sql = "select uid,sum(money) as total,m.username as username,m.realname,m.code,m.phone,m.memberRankId as rankid from mall_backmoney bb left join mall_member m on bb.uid = m.id where bb.level != 0 GROUP BY uid";
        long num = 0;
        Map<String,Object> result = new HashMap<String,Object>();
        BigDecimal availMoney = new BigDecimal(0);
        BigDecimal total = new BigDecimal(0);
        BigDecimal totalAvail = new BigDecimal(0);
        BigDecimal hasDrawNum = new BigDecimal(0);
        BigDecimal totalHasDrawNum = new BigDecimal(0);
        List<Record> lr = Db.find(sql);
        if(lr != null && lr.size()>0){
            for(Record r:lr){


                //计算已提现金额
                hasDrawNum = Db.queryBigDecimal("select sum(money) from mall_backmoney where uid = '"+r.getStr("uid")+"' and level = 0");
                if(hasDrawNum == null)
                    hasDrawNum = new BigDecimal(0);
                r.set("hasDrawNum",hasDrawNum.abs());
                totalHasDrawNum = totalHasDrawNum.add(hasDrawNum.abs());


                if(r.getStr("rankid")!=null && r.getStr("rankid").equalsIgnoreCase("sf60982c5faf48cb8f59a492a73a6b2c")){
                    r.set("freezeNum",r.getBigDecimal("total")).set("availNum",new BigDecimal(0));

                    total = total.add(r.getBigDecimal("total")==null?new BigDecimal(0):r.getBigDecimal("total"));
                    totalAvail = totalAvail.add(r.getBigDecimal("availNum")==null?new BigDecimal(0):r.getBigDecimal("availNum"));
                    continue;
                }
                num = Db.queryLong("select count(*) from mall_member where pids like '%"+r.getStr("uid")+"%' and memberRankId = 'sf60982c5faf48cb8f59a492a73a6b2c'");
                if(num >= 3){
                    r.set("availNum",r.getBigDecimal("total")).set("freezeNum",new BigDecimal(0));
                }
                if(num == 2){
                    availMoney = Db.queryBigDecimal("select sum(money) from mall_backmoney where level != 3 and uid = ? ",r.getStr("uid"));
                    r.set("availNum",availMoney).set("freezeNum", r.getBigDecimal("total").subtract(availMoney));
                }
                if(num == 1){
                    availMoney = Db.queryBigDecimal("select sum(money) from mall_backmoney where level = 1 and uid = ? ",r.getStr("uid"));
                    r.set("availNum",availMoney).set("freezeNum",r.getBigDecimal("total").subtract(availMoney));
                }
                total = total.add(r.getBigDecimal("total")==null?new BigDecimal(0):r.getBigDecimal("total"));
                totalAvail = totalAvail.add(r.getBigDecimal("availNum")==null?new BigDecimal(0):r.getBigDecimal("availNum"));

            }
        }

        Orders orders= Orders.dao.findFirst("select sum(totalAmount) totalAmount from mall_orders where paymentStatus='2' and uid=?",getLoginMember().getStr("id"));
        BigDecimal allTotalAmount=orders.getBigDecimal("totalAmount");
        if(allTotalAmount==null || "0".equals(allTotalAmount.toString())){
            allTotalAmount= new BigDecimal(0);
        }

        result.put("allTotalAmount",allTotalAmount);
        result.put("total",total);
        result.put("totalAvail",totalAvail.subtract(totalHasDrawNum));
        result.put("totalFreeze",total.subtract(totalAvail));
        result.put("totalHasDrawNum",totalHasDrawNum);
        result.put("data",lr);
        System.out.println(JsonKit.toJson(allTotalAmount));
        System.out.println(JsonKit.toJson(total));
        System.out.println(JsonKit.toJson(totalAvail.subtract(totalHasDrawNum)));
        System.out.println(JsonKit.toJson(total.subtract(totalAvail)));
        System.out.println(JsonKit.toJson(totalHasDrawNum));
        System.out.println(JsonKit.toJson(lr));
        renderJson(result);
    }*/
}
